﻿using MesGLApp.BLL;
using MesGLApp.DAL.RoleDTO;
using Smobiler.Core;
using Smobiler.Core.Controls;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;

namespace MesGLApp.View.Search
{
    ////ToolboxItem用于控制是否添加自定义控件到工具箱，true添加，false不添加
    //[System.ComponentModel.ToolboxItem(true)]
    partial class AbnormalOrderDetail : Smobiler.Core.Controls.MobileUserControl
    {
        private SqlSugarClient Db = SqlSugares.GetInstance();
        public AbnormalOrderDetail() : base()
        {
            //This call is required by the SmobilerUserControl.
            InitializeComponent();
        }
        private void UPDATE()
        {
            BaseUserDTO tmpUser = Client.Session["User"] as BaseUserDTO;
            //修改明细表状态
            Db.Ado.ExecuteCommand($@"update abnormal_detail set end_time = '{DateTime.Now}',update_user = '{tmpUser.user_name}',update_date = '{DateTime.Now}' where id = {id.Text}");

            //修改工单表状态
            var number = Db.SqlQueryable<object>($@"select
	                        *
                        from
	                        abnormal_detail
                        where
	                        end_time is null and prod_number = '{ItemOrderNo.Text}'").ToList();
            //没有异常明细的时候
            if (number.Count == 0)
            {
                //修改工单表状态
                Db.Ado.ExecuteCommand($@"update target_prod_process set err_status = 19 where prod_number = '{ItemOrderNo.Text}'");
            }
            statue_name.Text = "已解除！";
            button1.Visible = false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Press(object sender, EventArgs e)
        {
            MessageBox.Show("确认解除！", "提示", Smobiler.Core.Controls.MessageBoxButtons.OKCancel, (object s, MessageBoxHandlerArgs args) =>
            {
                if (args.Result.ToString() == "OK")
                {
                    string order = ItemOrderNo.Text;
                    //线二的严重异常只能由组长解除

                    BaseUserDTO tmpUser = Client.Session["User"] as BaseUserDTO;
                    if (statue_name.Text.ToString() == "严重异常" && ItemOrderNo.Text.Contains("GL02"))
                    {
                        var list = Db.Context.SqlQueryable<object>($@"select
	                        base_role_id
                        from
	                        base_user_role
                        where
	                        base_user_id = {tmpUser.id}").ToList();
                        dynamic dylist = list;
                        if (dylist[0].base_role_id != 26)
                        {
                            list = Db.Context.SqlQueryable<object>($@"SELECT STRING_AGG(T1.real_name, '、') AS concatenated_real_names
                            FROM base_user T1 
                            LEFT JOIN base_user_role T2 ON T1.id = T2.base_user_id
                            LEFT JOIN base_role T3 ON T3.id = T2.base_role_id
                            WHERE T3.id = 26;").ToList();
                            if (list.Count > 0)
                            {
                                dylist = list;
                                MessageBox.Show("不是品质组长，没有权限解除！请找组长" + dylist[0].concatenated_real_names);
                                return;
                            }
                            UPDATE();
                        }
                    }
                    UPDATE();
                }
            });
        }
    }
}